package acm.蓝桥16;

import java.util.Arrays;

public class G砖块摆放暴力 {
    /**
     * 贪心
     */
    public static void main(String[] args) {
        Reader sc = new Reader();
        n = sc.nextInt();
        arr = new int[n][2];
        for (int i = 0; i < arr.length; i++) {
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
        }
        Arrays.sort(arr,(a,b)->a[1]-b[1]);  //价值从小到大
        int dfs = dfs(0, 0);
        System.out.println(dfs);
    }

    static int[][] arr; //0位置重量，1位置价值
    static int n;

    public static int dfs(int i, int w) {
        if (i == n || arr[i][1] < w) {   //拿不了，后面的也拿不了
            return 0;
        }
        int no = dfs(i - 1, w);    //不拿
        int chose = dfs(i - 1, w + arr[i][0]) + arr[i][1];
        return Math.max(no, chose);
    }

}
